package org.lv.learning.service.impl;

import io.seata.spring.annotation.GlobalTransactional;
import org.lv.learning.client.AccountClient;
import org.lv.learning.client.StorageClient;
import org.lv.learning.entity.Order;
import org.lv.learning.mapper.OrderMapper;
import org.lv.learning.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 *
 */
@Service
public class OrderService implements IOrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private AccountClient accountClient;

    @Autowired
    private StorageClient storageClient;

    @Override
    @GlobalTransactional
    public Integer createOrder(Order order) {

        int orderInsert = orderMapper.insert(order);

        if(orderInsert > 0){
            // 扣减库存
            storageClient.deduct(order.getProductId(),order.getCount());
            // 扣减账户余额
            accountClient.deduct(order.getUserId(),order.getMoney());
        }

        return orderInsert;
    }
}
